package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lsx
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * TODO 获取所有课程 完善条件查询带分页
     * @return
     */
    @ApiOperation(value = "获取所有课程列表")
    @GetMapping("/list")
    public  R getCourseList(){
        List<EduCourse> courseList = eduCourseService.list(null);
        return  R.ok().data("courseList",courseList);
    }
    //分页接口
    @ApiOperation(value = "课程分页")
    @GetMapping("/pageCourse/{current}/{limit}")
    public  R pageCourseList(@PathVariable Long current , @PathVariable Long limit){
        //创建分页对象
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        //调用方法实现分页  底层封装 将分页的所有的数据封装到pageCourse中
        eduCourseService.page(pageCourse,null);

        /* try {
            int i = 10/0;
        } catch (Exception e) {
            throw  new GuliException(2000,"执行了自定义异常类");
        }*/

        long total = pageCourse.getTotal();//获取总记录数

        List<EduCourse> records = pageCourse.getRecords();//获取数据的list集合


        return R.ok().data("total",total).data("rows",records);
    }
    //多条件组合查询带分页
    @ApiOperation(value = "多条件组合查询带分页")
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current ,
                                  @PathVariable Long limit,
                                  //使用RequestBody注解 将前端请求的条件封装在teacherQuery对象中
                                  @RequestBody(required = false) CourseQuery courseQuery){

        Page<EduCourse> coursePage = new Page<>(current,limit);

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //写条件查询
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.like("status",status);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.like("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.like("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        eduCourseService.page(coursePage,wrapper);

        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();
        return R.ok().data("total",total).data("rows",records);

    }
    @DeleteMapping("{id}")
    public  R  deleteCourseById(@PathVariable String id){

        return  R.ok();
    }


    @PostMapping("/addCouserInfo")
    public  R addCouserInfo(@RequestBody CourseInfoVo courseInfoVo){

        String courseId = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }
    @GetMapping("/getCourse/{courseId}")
    public  R  getCourseById(@PathVariable String courseId){
       CourseInfoVo courseInfoVo =  eduCourseService.getCourseById(courseId);

        return  R.ok().data("courseInfoVo",courseInfoVo);
    }
    @PostMapping("/updateCourseInfo")
    public  R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    //根据课程id查询课程确认VO的信息
    @GetMapping("/CoursePublishInfo/{id}")
    public R getCoursePublishInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo=eduCourseService.getCoursePublishInfo(id);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }
    /**
     * 课程最终发布  实际事修改课程表中的status
     * 注:status中 Draft 未发布  Normal为以发布
     */
    @PostMapping("/publishCourse/{id}")
    public  R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }
    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();

    }
}

